﻿#include"include\Interfaces\ImageLoader.h"
#include<QPainter>
#include<QDebug>
#include<QThread>
#include"include\Interfaces\BinocularCamera.h"

#include <QTimer>
Imageloader::Imageloader(QWidget*parent) 
	:QOpenGLWidget(parent)
{
	qDebug() << "Imageloader::Imageloader " << parent->width() << parent->height();
	this->resize(parent->width(), parent->height());					//_loader->resize(ui->label)载入初始化的label大小；（492,315）


    clear();//--added by qinyaning 202008251042 

	_pen.setColor("#88ff4d");
	_pen.setWidth(4);
}

Imageloader::~Imageloader() {
}
bool Imageloader::load(const QString &fileName) {
	bool ret = false;
	if (image.load(fileName)) {
		update();// repaint widget
		ret = true;
	}
	//    qDebug()<<"imageloader: "<<fileName;
	return ret;
}
bool Imageloader::load(cv::Mat&src) {
	bool ret = false;
	////qDebug() << "SrcMat type: " << src.type();
	//image = mat2QImage(src).convertToFormat(QImage::Format_RGB888);
	//if (!image.isNull()) {
	//	qDebug() << "show image: "<< QThread::currentThreadId();
	//	update();
	//	ret = true;
	//}
	return ret;
}

bool Imageloader::load(const QImage&pupilImage) {
	//qDebug() << QString("width: %1 height: %2").arg(pupilImage.width()).arg(pupilImage.height());
    //QThread::msleep(1);//commented by qinyaning 202009181536

	bool ret = false;
	if (!pupilImage.isNull()) {
		image = pupilImage.convertToFormat(QImage::Format_RGB888);
		update();
		ret = true;
	}
	return ret;
}

bool Imageloader::load(QIODevice*device, const char* format) {
	bool ret = false;
	if (image.load(device, format)) {
		update();// repaint widget
		ret = true;
	}
	return ret;
}

bool Imageloader::loadFromData(const uchar *data, int len, const char *format) {
	bool ret = false;
	if (image.loadFromData(data, len, format)) {
		update();// repaint widget
		ret = true;
	}
	return ret;
}
bool Imageloader::loadFromData(const QByteArray &data, const char *format) {
	bool ret = false;
	if (image.loadFromData(data, format)) {
		update();// repaint widget
		ret = true;
	}
	return ret;
}
void Imageloader::clear() {
	image = QImage(":/icon/black_background.jpg");
	update();
}
void Imageloader::paintEvent(QPaintEvent *e) {
	//qDebug() << "Imageloader::paintEvent";

	if (!this->image.isNull()) {

		QPainter painter;
		painter.begin(this);
        painter.drawImage(this->rect(), this->image.scaled(this->width(),
                                                          this->height(),
                                                          Qt::IgnoreAspectRatio,
                                                          Qt::SmoothTransformation));
		painter.setPen(_pen);	//设置画笔
		painter.drawEllipse(QPointF(width()/2,height()/2),20,20);	//椭圆
		painter.end();
	}
}


